package com.hengpeng.itfin.job.draw;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import com.hengpeng.common.cis.ComponentContextLoader;
import com.hengpeng.common.scheduling.AbstractJob;
import com.hengpeng.common.scheduling.ScheduleConfig;
import com.hengpeng.itfin.domain.draw.common.Constants;
import com.hengpeng.itfin.domain.draw.common.FileCSVHelper;
import com.hengpeng.itfin.domain.draw.common.PayUtil;
import com.hengpeng.itfin.service.ServiceFactory;
import com.hengpeng.itfinbase.persist.Drawing;

/**
 * <pre>
 *  生成提款文件任务
 * </pre>
 * @author shanguogang
 * @version 1.0, 2015-8-12
 */
public class CreateCsvJob extends AbstractJob
{

	/**
	 * <pre>
	 * 创建提款文件任务:(该任务忽略超时处理)
	 * 首先 获取自动提款支付商列表
	 * 然后 根据支付商ID获取未处理提款记录
	 * 然后 生成提款文件
	 * </pre>
	 * @param isTimeout 超时标识
	 */
	@Override
	public void exec(boolean isTimeout, ScheduleConfig config)
	{
		ServiceFactory serviceFactory = ComponentContextLoader.getBean(ServiceFactory.class);
		serviceFactory.getDrawFileService().requireNeverCreateDrawFile();

	}

	public static boolean testCreateCSV()
	{
		// 获取未处理提款记录
		List<Drawing> list = new ArrayList<Drawing>();
		int size = 2;
		for (int i = 0; i < size; i++)
		{
			Drawing bean = new Drawing();
			bean.setBankCardNo("11111111");
			bean.setBankName("中国银行");
			bean.setUserName("shanguogang11");
			bean.setUserId(143815188410700032l);
			bean.setBranch("深圳南山支行");
			bean.setCity("深圳市");
			bean.setProvince("广东省");
			bean.setMoney(200l);
			bean.setOrdId("143936465410501347");
			list.add(bean);
		}
		if (list != null && list.size() > 0)
		{
			boolean success = true;
			StringBuffer contentBuf = new StringBuffer();

			StringBuffer recordBuf = new StringBuffer();

			recordBuf.append(Constants.ALIPAY_UPLOAD_TABLE_TITLE_SEQ).append(Constants.ALIPAY_SEPARATOR);
			recordBuf.append(Constants.ALIPAY_UPLOAD_TABLE_TITLE_USERNAME).append(Constants.ALIPAY_SEPARATOR);
			recordBuf.append(Constants.ALIPAY_UPLOAD_TABLE_TITLE_BANKACOUNT).append(Constants.ALIPAY_SEPARATOR);
			recordBuf.append(Constants.ALIPAY_UPLOAD_TABLE_TITLE_BANKNAME).append(Constants.ALIPAY_SEPARATOR);
			recordBuf.append(Constants.ALIPAY_UPLOAD_TABLE_TITLE_PROVINCE).append(Constants.ALIPAY_SEPARATOR);
			recordBuf.append(Constants.ALIPAY_UPLOAD_TABLE_TITLE_CITY).append(Constants.ALIPAY_SEPARATOR);
			recordBuf.append(Constants.ALIPAY_UPLOAD_TABLE_TITLE_BRANCHNAME).append(Constants.ALIPAY_SEPARATOR);
			recordBuf.append(Constants.ALIPAY_UPLOAD_TABLE_TITLE_MONEY).append(Constants.ALIPAY_SEPARATOR);
			recordBuf.append(Constants.ALIPAY_UPLOAD_TABLE_TITLE_FLAG).append(Constants.ALIPAY_SEPARATOR);
			recordBuf.append(Constants.ALIPAY_UPLOAD_TABLE_TITLE_NOTE).append("\r\n");
			// 生成提款文件
			String sysDatetime = PayUtil.getCurrDate();
			int totalRecord = list.size();
			float totalMoney = 0;
			for (Drawing bean : list)
			{
				recordBuf.append(bean.getOrdId()).append(Constants.ALIPAY_SEPARATOR);

				recordBuf.append(bean.getRealName()).append(Constants.ALIPAY_SEPARATOR);
				recordBuf.append(bean.getBankCardNo()).append(Constants.ALIPAY_SEPARATOR);
				recordBuf.append(bean.getBankName()).append(Constants.ALIPAY_SEPARATOR);
				recordBuf.append(bean.getProvince()).append(Constants.ALIPAY_SEPARATOR);
				recordBuf.append(bean.getCity()).append(Constants.ALIPAY_SEPARATOR);
				DecimalFormat df = new DecimalFormat("#0.00");
				Double d = Double.valueOf(bean.getMoney());
				double betmoney = new Double(df.format(d / 100));
				double money = PayUtil.formatDouble(PayUtil.recoup(betmoney));
				recordBuf.append(PayUtil.recoupBankAddress(money, bean.getSubbranch())).append(
						Constants.ALIPAY_SEPARATOR);
				// 扣除提现手续费
				recordBuf.append(money).append(Constants.ALIPAY_SEPARATOR);
				totalMoney += money;

				recordBuf.append(2).append(Constants.ALIPAY_SEPARATOR);
				recordBuf.append("mark:" + bean.getUserName());
				recordBuf.append("\r\n");
			}
			/** ************ 概要 **************************************** */
			contentBuf.append(Constants.ALIPAY_UPLOAD_MAIN_TITLE_DATE).append(Constants.ALIPAY_SEPARATOR);
			contentBuf.append(Constants.ALIPAY_UPLOAD_MAIN_TITLE_TOTALMONEY).append(Constants.ALIPAY_SEPARATOR);
			contentBuf.append(Constants.ALIPAY_UPLOAD_MAIN_TITLE_TOTALRECORD).append(Constants.ALIPAY_SEPARATOR);
			contentBuf.append(Constants.ALIPAY_UPLOAD_MAIN_TITLE_ALIPAYACOUNT);
			contentBuf.append("\r\n");

			contentBuf.append(sysDatetime.substring(0, 8)).append(Constants.ALIPAY_SEPARATOR);
			contentBuf.append(totalMoney).append(Constants.ALIPAY_SEPARATOR);
			contentBuf.append(totalRecord).append(Constants.ALIPAY_SEPARATOR);
			contentBuf.append("happycp@szhelper.com");
			contentBuf.append("\r\n");
			contentBuf.append(recordBuf);

			log.info("共提取数据" + totalRecord + "笔");

			String csvfile = sysDatetime + Constants.POSTFIX_CSV;
			// 查检路径,没有则自动创建
			String path = null;
			try
			{
				path = FileCSVHelper.checkPath("D:\\upload\\");
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}

			/** ***************** 生成CSV文件 *********************************** */

			success = FileCSVHelper.generateCSVFile(path + csvfile, contentBuf.toString());

			if (!success)
			{
				log.info("创 建提现上传文件 [" + csvfile + "] 失败.");
				return false;
			}
			else
			{
				log.info("创 建上传文件 [" + csvfile + "] 成功.");
				return true;
			}
		}
		return false;
	}

}
